#!/bin/bash
echo "cassandra install is starting @ ${HOSTNAME}"
sudo rm /etc/yum.repos.d/*.repo
sudo sed -i "s/enabled=1/enabled=0/g" /etc/yum/pluginconf.d/fastestmirror.conf
configpath=/etc/yum.repos.d/infra.repo 
sudo  tee -a ${configpath}  > /dev/null  << TEEEOF
[centos]
baseurl = http://sz.viper.sensesecurity.net/yum/centos/
gpgcheck = 0
name = Centos repository

[epel]
baseurl = http://sz.viper.sensesecurity.net/yum/epel/
gpgcheck = 0
name = Epel repository

[docker-ce]
baseurl = http://sz.viper.sensesecurity.net/yum/docker-ce/
gpgcheck = 0
name = Docker repository

[nvidia-docker]
baseurl = http://sz.viper.sensesecurity.net/yum/nvidia-docker/
gpgcheck = 0
name = Nvidia Docker repository

[cuda]
baseurl = http://sz.viper.sensesecurity.net/yum/cuda/
gpgcheck = 0
name = CUDA repository

[infra]
baseurl = http://sz.viper.sensesecurity.net/yum/infra/
gpgcheck = 0
name = infra repository
TEEEOF

sudo yum -q clean all
sudo rm -rf /var/cache/yum
sudo yum -q makecache
sudo yum -q install java nmap-ncat telnet net-tools wget tcpflow tcpdump jq supervisor -y
sudo mkdir  -p  /data
wget -qO - http://sz.viper.sensesecurity.net/yum/infra/apache-cassandra-3.11.2-bin.tar.gz |sudo tar zxf  -  -C /data
sudo ln -s /mnt/b /data/apache-cassandra-3.11.2/data
sudo sed -i "s/127.0.0.1/192.168.122.11/g" /data/apache-cassandra-3.11.2/conf/cassandra.yaml
sudo sed -i "s/# listen_interface: eth0/listen_interface: eth1/g" /data/apache-cassandra-3.11.2/conf/cassandra.yaml
sudo sed -i "s/listen_address: localhost/#listen_address: localhost/g" /data/apache-cassandra-3.11.2/conf/cassandra.yaml
sudo sed -i "s/# rpc_interface: eth0/rpc_interface: eth1/g" /data/apache-cassandra-3.11.2/conf/cassandra.yaml
sudo sed -i "s/rpc_address: localhost/#rpc_address: localhost/g" /data/apache-cassandra-3.11.2/conf/cassandra.yaml
sudo sed -i "s/# JVM_OPTS=\"\$JVM_OPTS -Djava.rmi.server.hostname=<public name>\"/JVM_OPTS=\"\$JVM_OPTS -Djava.rmi.server.hostname=0.0.0.0\"/g" /data/apache-cassandra-3.11.2/conf/cassandra-env.sh
sudo sed -i "s/jmxremote.authenticate=true/jmxremote.authenticate=false/g" /data/apache-cassandra-3.11.2/conf/cassandra-env.sh
sudo sed -i "/7199/ a LOCAL_JMX=\"no\""  /data/apache-cassandra-3.11.2/conf/cassandra-env.sh
sudo wget -q http://central.maven.org/maven2/mx4j/mx4j-tools/3.0.1/mx4j-tools-3.0.1.jar -O /data/apache-cassandra-3.11.2/lib/mx4j-tools.jar

tokens=`lsblk -b   |grep  -E "vdb|vdc"  |grep -v  disk|awk '{sum+=$4}END{print int(sum/10000000000+0.5)}'`
echo "$HOSTNAME set tokens $tokens"
sed -i "s/num_tokens: 256/num_tokens: ${tokens}/" /data/apache-cassandra-3.11.2/conf/cassandra.yaml

#cassandra daemon configure
configpath=/etc/supervisord.d/cassandra.ini
sudo  tee -a ${configpath}  > /dev/null  << TEEEOF
[program:cassandra]
command=sudo ./cassandra -f -R
directory=/data/apache-cassandra-3.11.2/bin
autorestart=true
autostart=true
user=root
stderr_logfile = /var/log/cassandra.log
stdout_logfile = /var/log/cassandra_error.log
startretries = 9999
startsecs = 5

TEEEOF
sudo systemctl enable supervisord
sudo systemctl start supervisord
sudo supervisorctl reload
